using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using HandyControl.Tools.Extension;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;

namespace CodeGenerator.utils;

public class Tools {
    // 用于存储所有文件路径
    public static List<string> files = new List<string>();


    public static MainWindow MainWindow;


    public static void GetAllFiles(string[] paths, string[] types) {
        HashSet<string> typesSet = new HashSet<string>(types);
        foreach (string path in paths) {
            GetDirFiles(path, typesSet);
        }
    }

    private static void GetDirFiles(string path, HashSet<string> typesSet) {
        if (string.IsNullOrEmpty(path)) {
            return;
        }

        // 判断path是否是文件
        if (File.Exists(path)) {
            // 判断是否是指定类型
            string type = path.Substring(path.LastIndexOf('.') + 1);
            if (typesSet.Contains(type)) {
                files.Add(path);
            }
        }
        else if (Directory.Exists(path)) {
            // 是文件夹
            string[] files = Directory.GetFiles(path);
            foreach (string file in files) {
                GetDirFiles(file, typesSet);
            }

            string[] dirs = Directory.GetDirectories(path);
            foreach (string dir in dirs) {
                GetDirFiles(dir, typesSet);
            }
        }
    }


    public static double convertProgress(double val, double maxval) {
        return val / maxval * 100;
    }

    public static List<string> ReadAllLines() {
        List<string> lines = new List<string>();
        MainWindow.Dispatcher.Invoke(() => {
            MainWindow.LabelTask.Content = "当前任务  (文件读取)  进度: ";
        });
        
        int read = 0;
        foreach (string file in files) {
            string[] allLines = File.ReadAllLines(file);

            // 去除空行

            var list = allLines.Where(row => {
                // python 注释
                row = row.Trim();
                if (row.StartsWith("#")) {
                    return false;
                }

                // c# 注释
                if (row.StartsWith("//")) {
                    return false;
                }

                // 空行
                if (string.IsNullOrEmpty(row)) {
                    return false;
                }

                return true;
            }).ToList();


            lines.AddRange(list);
            read++;
            MainWindow.Dispatcher.Invoke(() => {
                MainWindow.ProgressBar.Value = convertProgress(read, files.Count);
            });
            
        }

        return lines;
    }


    public static void GenerateDocx(List<string> contents, string savePath) {
       
        
        MainWindow.Dispatcher.Invoke(() => {
            MainWindow.LabelTask.Content = "当前任务  (生成Word文档)  进度: ";
            MainWindow.ProgressBar.Value = 0;
        });
        Document doc = new Document();
        //新增水印页，后续会进行删除
        //doc.AddSection();
        //新增内容页,开始写入真实内容
        Section section = doc.AddSection();
        
        // 增加行号, 从1开始, 间隔1
        doc.Sections[0].PageSetup.LineNumberingStartValue = 1;
        doc.Sections[0].PageSetup.LineNumberingStep = 1;
        doc.Sections[0].PageSetup.LineNumberingDistanceFromText = 30f;
        doc.Sections[0].PageSetup.LineNumberingRestartMode = LineNumberingRestartMode.RestartPage;
        int read = 0;
        Console.WriteLine("开始写入内容");
        foreach (string content in contents) {
            Paragraph paragraph = section.AddParagraph();
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            TextRange textRange = paragraph.AppendText(content);
            textRange.CharacterFormat.FontSize = 11f;
            textRange.CharacterFormat.FontName = "Times New Roman";
            paragraph.Format.LineSpacing = 15f;
            read++;
            MainWindow.Dispatcher.Invoke(() => {
                MainWindow.ProgressBar.Value = convertProgress(read, contents.Count);
            });
            
        }
        Console.WriteLine("内容写入完成");

        // 去水印
        // doc.Sections.RemoveAt(0);
        Console.WriteLine("开始保存");
        doc.SaveToFile(savePath, FileFormat.Docx2013);
        doc.Close();
        Console.WriteLine("保存成功");
    }
}